package com.baiku.sanhos.controller;

import com.baiku.sanhos.mapper.FeedBackMapper;
import com.baiku.sanhos.pojo.dto.MessageInfoDTO;
import com.baiku.sanhos.pojo.entity.FeedBack;
import com.baiku.sanhos.pojo.vo.FeedBackVO;
import com.baiku.sanhos.response.ResultVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/pa/feedback")
public class FeedBackController {
    @Autowired
    FeedBackMapper feedBackMapper;

    /**
     * 留言反馈分页查询
     * @param feedBack
     * @return
     */

    @PostMapping("/selectAll")
    public ResultVO selectAll(@RequestBody FeedBack feedBack) {
        int pageSize = feedBack.getPageSize();
        int page = feedBack.getPage();
        Map<String, Object> map = new HashMap<>();
        int total = feedBackMapper.getTotalFeedBack(feedBack.getTheme(), feedBack.getUserId());//总条数
        int totalPages = (int) Math.ceil((double) total / pageSize);//计算总页数
        List<FeedBack> feedBackList;
        if (totalPages == 0) {
            feedBackList = new ArrayList<>();
        } else {
            int offset = (page - 1) * pageSize;
            feedBackList = feedBackMapper.selectAll(pageSize, offset, feedBack.getTheme(), feedBack.getNickName());
        }
        map.put("feedBack", feedBackList);
        map.put("total", total);
        return ResultVO.ok(map);
    }

    /**
     * 删除留言
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    public ResultVO deleteById(@PathVariable Integer id) {
        feedBackMapper.deleteById(id);
        return ResultVO.ok();
    }

    /**
     *批量删除留言
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteByIds")
    public ResultVO deleteByIds(@RequestBody List<Integer> ids){
        feedBackMapper.deleteByIds(ids);
        return ResultVO.ok();
    }
    @PostMapping("/updateById")
    public ResultVO updateById(@RequestBody FeedBackVO feedBackVO){
        FeedBack feedBack = new FeedBack();
        feedBackMapper.updateById(feedBackVO);
        BeanUtils.copyProperties(feedBackVO,feedBack);
        return ResultVO.ok();
    }

    @ApiOperation("发送反馈消息")
    @PostMapping("/send")
    @PreAuthorize("hasAnyAuthority('Details')")
    public ResultVO sendMessage(@RequestBody MessageInfoDTO messageInfoDTO){
        FeedBack feedBack=new FeedBack();
        messageInfoDTO.setStatus(0);
        BeanUtils.copyProperties(messageInfoDTO,feedBack);
        feedBackMapper.insert(feedBack);
        return ResultVO.ok();
    }
}
